<template>
  <div class="app-container">
    <div class="searchBox">
      <el-form :inline="true" :model="search">
        <el-form-item label="版本号" prop="version">
          <el-input
            v-model="search.version"
            placeholder="请输入版本号"
            clearable
            size="small"
            @keyup.enter.native="searchQuery"
          />
        </el-form-item>
        <el-form-item style="margin-top: -2px">
          <el-button type="primary" @click="searchQuery">搜索</el-button>
          <el-button
            type="info"
            @click="
              search = {};
              searchQuery();
            "
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <qm-table
      :data="query"
      keyProp="versionId"
      menuType="text"
      :showCheckbox="false"
      useFormPage
      :removeFunc="removeFunc"
       ref="qmTable"
    >
      <qm-table-column field="version">版本号</qm-table-column>
      <qm-table-column field="versionDescribe">版本描述</qm-table-column>
      <qm-table-column field="pubdate">发布时间</qm-table-column>
      <qm-table-column field="createUserName">创建人名称</qm-table-column>
      <qm-table-column field="createTime" fieldType="date"
        >创建时间</qm-table-column
      >
    </qm-table>
  </div>
</template>

<script>
import request from "@/utils/request";
import Controller from "@/libs/framework/controller";
export default class Instance extends Controller {
  onBeforeInit() {
    this.urls = {
      model: "/eip/marketAppVersion/getListModel",
      query: "/eip/marketAppVersion/query",
    };
  }

  onBeforeQuery(type, conditions, params) {
    if (type === "list") {
      this.$util.extend(params.query, { appId: this.vm.appId });
    }
  }

  onAfterGetEntity(res) {
    const { pageAction, models } = res;
    if (pageAction === "create") {
      (models.model || {}).appId = this.vm.appId;
      models.uploadSuccessFiles = [];
    }
  }

  onBeforeSave(postModel) {
    try {
      const { pageAction, uploadSuccessFiles } = postModel;
      if (
        pageAction === "create" &&
        Array.isArray(uploadSuccessFiles) 
      ) {
        postModel.attachmentIds = (
          uploadSuccessFiles.map((item) => {
            return item.attachmentId;
          }) || []
        ).join(",");
      }
      delete postModel.uploadSuccessFiles;
    } catch (error) {
      return false;
    }
    return true;
  }

  mixin() {
    return {
      props: {
        appId: String,
      },
      methods: {
         removeFunc(rows) {
          this.$confirm("删除会一并删除与之关联的数据(问题、答复)，是否继续删除？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              let versionIds=rows.map(item=>{
                return {versionId:item.versionId}
              })
              request({
                url: "/eip/marketAppVersion/delete",
                method: "post",
                data: {
                  delete: JSON.stringify(versionIds)
                },
              })
                .then((r) => {
                  if (r ) {
                    this.$message({
                      type: "success",
                      message: "删除成功!",
                    });
                    this.$refs["qmTable"].refresh();
                  } else {
                    this.$message.error("数据请求失败");
                  }
                })
                .catch((e) => {
                  this.$message.error(e);
                });
            })
            .catch(() => {});
        },
      },
    };
  }
}
</script>
